package com.bwf.redirect;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/6/8 15:58
 * @Copyright 博为峰
 */
public class ServletDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ServletDemo01");
        //------响应---------------------------------
        resp.setHeader("r01", "r01");
        PrintWriter printWriter = resp.getWriter();
        printWriter.write("<h1>r01</h1>");
        //------设置数据---------------------------------
        req.setAttribute("servletName", "ServletDemo01");
        //------重定向---------------------------------
//        resp.setHeader("Location","/web03_war_exploded/r02");
//        resp.setStatus(302);
        //获取项目路径
        String contextPath = req.getContextPath();
//        resp.sendRedirect(contextPath + "/r02");
        resp.sendRedirect("https://www.atstudy.com/");
    }
}
